package com.mj.web;


import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;
import java.net.URI;
import java.rmi.server.UnicastRemoteObject;

public class FluxMainApplication {

    public static void main(String[] args) throws IOException {
//快速编写一个数据处理服务器
   //1. 创建一个数据处理http服务器 ,参数:请求,相应,返回值:Mono<void >代表处理完成的信号
        HttpHandler httpHandler = (request, response)->{
            response.getHeaders().add("Content-Type","text/plain");
            URI uri = request.getURI();
//获取响应头
//        response.getHeaders();
        //获取Cookie
//        response.getCookies();
        //获取状态码`
//        response.getStatusCode();
        //获取响应体
//        response.bufferFactory();
        //写入响应体
//        response.writeWith( );
        //设置完成信号
//            response.setComplete();
//数据的发布者:Mono<DataBuffer>,Flux<DataBuffer>
            //创建相应数据的工场dataBuffer
          DataBufferFactory factory= response.bufferFactory();
          //创建数据
          DataBuffer buffer=factory.wrap(new String(uri.toString()).getBytes());
//需要一个DataBuffer发布者
        return response.writeWith( Mono.just(buffer));
        };
//2.启动一个服务区,监听80880 接受数据 拿到数据交给HttpHandler处理
        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
 //3.启动Netty服务器
        HttpServer.create()
                .host("localhost")
                .port(8080)
                //绑定处理程序
                .handle(adapter)
                .bindNow();

        System.out.println("启动成功");
        //System.in.read();
        System.out.println("服务器停止");

    }
}
